package cn.qiao.benchmark;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.support.ConnectionPoolSupport;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

/**
 * desc:
 *
 * @author qiao
 * @date 2024年03月07日 22:43
 */
public class Test {
    public static void main(String[] args) {
        LettucePoolImpl lettucePool = new LettucePoolImpl(getLettucePool());
        lettucePool.set("key","13");
        System.out.println(lettucePool.get("key"));
    }

   public static JedisPool getJedisPool(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10); // 设置最大连接数
        jedisPoolConfig.setMaxIdle(5);   // 设置最大空闲连接数
        // 创建 Jedis 连接池
        return new JedisPool(jedisPoolConfig, "localhost", 6379,3,null,3);
    }

    public static GenericObjectPool getLettucePool(){
        RedisURI redisUri = RedisURI.builder()
                .withHost("127.0.0.1")
                .withPort(6379)
                .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
                .build();
        RedisClient client = RedisClient.create(redisUri);
        //连接池配置
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(100);
        GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(client::connect, poolConfig);
        return pool;
    }

}
